﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ToolKit.Mvvm.Extensions
{
    public static class TypeExtension
    {
        /// <summary>
        /// 获取Type的构造函数，返回参数最多的构造函数
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="maps"></param>
        /// <returns></returns>
        public static ConstructorInfo GetConstructorInfo(this Type serviceType, ConcurrentDictionary<Type, Type> _interfaceMaps)
        {
            Type t;

            if (_interfaceMaps.ContainsKey(serviceType))
            {
                t = _interfaceMaps[serviceType] ?? serviceType;
            }
            else
            {
                t = serviceType;
            }

            var constructorInfos = t.GetConstructors();

            return constructorInfos.OrderByDescending(c => c.GetParameters().Length).FirstOrDefault();
        }
    }
}
